<?php

/**
 * This sample app is provided to kickstart your experience using Facebook's
 * resources for developers.  This sample app provides examples of several
 * key concepts, including authentication, the Graph API, and FQL (Facebook
 * Query Language). Please visit the docs at 'developers.facebook.com/docs'
 * to learn more about the resources available to you
 */

// Provides access to app specific values such as your app id and app secret.
// Defined in 'AppInfo.php'
require_once('AppInfo.php');

// Enforce https on production
if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
  header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  exit();
}

// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');


/*****************************************************************************
 *
 * The content below provides examples of how to fetch Facebook data using the
 * Graph API and FQL.  It uses the helper functions defined in 'utils.php' to
 * do so.  You should change this section so that it prepares all of the
 * information that you want to display to the user.
 *
 ****************************************************************************/

require_once('sdk/src/facebook.php');

$facebook = new Facebook(array(
  'appId'  => AppInfo::appID(),
  'secret' => AppInfo::appSecret(),
  'sharedSession' => true,
  'trustForwarded' => true,
));

$signed_request = $_REQUEST["signed_request"];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

$app_data = isset($data["app_data"]) ? $data["app_data"] : '';
$_REQUEST["fb_page_id"] = $data["page"]["id"];
$access_admin = $data["page"]["admin"] == 1;
$has_liked = $data["page"]["liked"] == 1;

// Fetch the basic info of the app that they are using
$app_info = $facebook->api('/'. AppInfo::appID());

$app_name = idx($app_info, 'name', '');

?>
<!DOCTYPE html>
<html xmlns:fb="http://ogp.me/ns/fb#" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />

    <title><?php echo he($app_name); ?></title>
    <link rel="stylesheet" href="stylesheets/screen.css" media="Screen" type="text/css" />

    <meta property="og:title" content="<?php echo he($app_name); ?>" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="<?php echo AppInfo::getUrl(); ?>" />
    <meta property="og:image" content="<?php echo AppInfo::getUrl('/logo.png'); ?>" />
    <meta property="og:site_name" content="<?php echo he($app_name); ?>" />
    <meta property="og:description" content="My first app" />
    <meta property="fb:app_id" content="<?php echo AppInfo::appID(); ?>" />

    <script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>
	<style>
    body
    {
        overflow: hidden;
        width : 800px;
	    position: relative;
	    text-align:center;
        /* your styles here... */
    }
    h1
    {
    	color: Navy;
    	font-size:x-large;
    	font-weight:bold;
    }
    </style>
	<script>
		var HerokuUrl = '<?php echo AppInfo::getUrl(); ?>';
		$(document).ready(function() {
			var url = (window.location != window.parent.location) ? document.referrer: document.location;
            var mySplitResult = url.split("/");

			if(mySplitResult[2] != "static.ak.facebook.com" && mySplitResult[2] != "s-static.ak.facebook.com")
            {
                window.parent.location = HerokuUrl + "redirect.php";
            }
		});
	</script>
  </head>
  <body>
    <?php 
	if(!isset($_REQUEST["signed_request"]))
	{ ?>
	<script>
		$(document).ready(function() {
			var url = (window.location != window.parent.location) ? document.referrer: document.location;
            var mySplitResult = url.split("/");
			var HerokuUrl = '<?php echo AppInfo::getUrl(); ?>';
			window.parent.location = HerokuUrl + "redirect.php";
		});
	</script>
	<?php 
	} else if($has_liked) {?>
    <div id="fb-root"></div>
    <div style="display:none"><fb:login-button autologoutlink='true'               
scope='email,user_birthday,status_update,publish_stream,friends_hometown,friends_location,read_stream,user_photos,friends_photos,user_photo_video_tags,friends_photo_video_tags'></fb:login-button></div>

	<iframe id="iframe" src="http://themes.goodlayers.com/grandcollege" frameborder="0" width="100%"></iframe>
	
	<?php
	} 
	else 
	{ ?>
	   <img src="images/like.png" />
	   <div>
			<a target="_blank" href="http://vwsol.com">powered by vietwing</a>
	   </div>
	<?php } ?>
  </body>
</html>
